﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Reader.Api
{
    public class Action
    {

        public static string action(Dictionary<string, string> d)
        {
            string imei, version = null, model = null, os = null, os_name = null, screen_size = null, type = null;
            string JsonStr = "{\"success\": false, \"result\": {}}";
            string action = d["action"];
            try { imei = d["imei"]; }
            catch { imei = ""; }
            try { version = d["version"]; }
            catch { version = ""; }
            try { model = d["model"]; }
            catch { model = ""; }
            try { os = d["os"]; }
            catch { os = ""; }
            try { os_name = d["os_name"]; }
            catch { os_name = ""; }
            try { screen_size = d["screen_size"]; }
            catch { screen_size = ""; }
            try { type = d["type"]; }
            catch { type = ""; }

            if (action.Equals("update"))
                JsonStr = getUpdate(type);
            else if (action.Equals("message"))
                JsonStr = getMessage(type);
            else if (action.Equals("install"))
                JsonStr = addInstall(type, imei, version, model, os, os_name, screen_size);
            else
                JsonStr = "{\"success\": false, \"result\": {}}";
            return JsonStr;

        }

        private static string getUpdate(String type)
        {
            string JsonStr = "{\"success\": false, \"result\": {}}";
            DataSet ds = DB.getUpdate(type);
            try
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    float version = float.Parse(ds.Tables[0].Rows[0]["version"].ToString());
                    string title = ds.Tables[0].Rows[0]["title"].ToString();
                    string body = ds.Tables[0].Rows[0]["body"].ToString();
                    string url = ds.Tables[0].Rows[0]["url"].ToString();
                    string update = ds.Tables[0].Rows[0]["update"].ToString();
                    JsonStr = "{\"success\": true, \"result\": {\"title\":\""
                    + AdinZ.Common.Unicode.gbToUnicode(title) + "\",\"body\":\""
                    + AdinZ.Common.Unicode.gbToUnicode(body) + "\",\"url\":\"" + url
                    + "\",\"version\":" + version + ",\"update\":\""
                    + update + "\"}}";
                }
            }
            catch  { JsonStr = "{\"success\": false, \"result\": {}}"; }
            return JsonStr;
        }


        private static string getMessage(string type)
        {
            string JsonStr = "{\"success\": false, \"result\": {}}";
            DataSet ds = DB.getMessage(type);
            try
            {

                if (ds.Tables[0].Rows.Count > 0)
                {
                    string title = ds.Tables[0].Rows[0]["title"].ToString();
                    string body = ds.Tables[0].Rows[0]["body"].ToString();
                    string enddate = ds.Tables[0].Rows[0]["enddate"].ToString();
                    string update = ds.Tables[0].Rows[0]["update"].ToString();
                    string imei = ds.Tables[0].Rows[0]["imei"].ToString();
                    JsonStr = "{\"success\": true, \"result\": {\"title\":\""
                            + AdinZ.Common.Unicode.gbToUnicode(title) + "\",\"body\":\""
                            + AdinZ.Common.Unicode.gbToUnicode(body) + "\",\"enddate\":\""
                            + enddate + "\",\"update\":\"" + update + "\",\"imei\":\"" + imei + "\"}}";
                }
            }
            catch {}
            return JsonStr;
        }

        private static string addInstall(string type, string imei, string version, string model, string os, string os_name, string screen_size)
        {
            string JsonStr = "{\"success\": false, \"result\": 0}";
            int i = 0;
            if (imei.Length < 3)
            {
                return JsonStr;
            }
            else
            {
                if (!DB.isExistInstall(imei, type))
                {
                    i = DB.addInstall(type, imei, version, model, os, os_name, screen_size);
                    JsonStr = "{\"success\": true, \"result\": " + i + "}";
                }
                else
                    JsonStr = "{\"success\": false, \"result\": 1}";
                return JsonStr;
            }
        }
    
    
    
    
    
    
    
    
    
    
    }
}
